﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using EZCRS.HCMS.Data;

namespace EZCRS.HCMS.SqlClient
{
    public class sql_AdminCompanyUser:MainBase
    {
        /// <summary>
        /// 添加或修改公司客信息
        /// </summary>
        /// <param name="usrid">用户ID</param>
        /// <param name="corpid">所属公司ID</param>
        /// <param name="usrnumb">用户卡号</param>
        /// <param name="usrpass">用户密码</param>
        /// <param name="usrstatus">用户是否锁定</param>
        /// <param name="inuser">创建人</param>
        /// <returns></returns>
        public bool CompanyUser_Add(int usrid, int corpid, string usrnumb, string usrpass, string usrstatus, string inuser, int gid, int pid, string createtime)
        {
            string SqlStr;
            if (usrid == 0)
            {
                SqlStr = "insert into [hfg_company_user] (corpid,usrnumb,usrpass,usrstatus,indate,inuser,gid,pid) values (@corpid,@usrnumb,@usrpass,@usrstatus,@indate,@inuser,@gid,@pid)";
            }
            else
            {
                SqlStr = "update [hfg_company_user] set corpid=@corpid,usrnumb=@usrnumb,usrstatus=@usrstatus,indate=@indate,inuser=@inuser,gid=@gid,pid=@pid where usrid='" + usrid + "'";
            }
            SqlParameter[] par = new SqlParameter[8];
            par[0] = new SqlParameter("@corpid", corpid);
            par[1] = new SqlParameter("@usrnumb", usrnumb);
            par[2] = new SqlParameter("@usrpass", usrpass);
            par[3] = new SqlParameter("@usrstatus", usrstatus);
            if (usrid == 0)
            {
                par[4] = new SqlParameter("@indate", DateTime.Now.ToString());
            }
            else
            {
                par[4] = new SqlParameter("@indate", createtime);
            }
            par[5] = new SqlParameter("@inuser", inuser);
            par[6] = new SqlParameter("@gid", gid);
            par[7] = new SqlParameter("@pid", pid);
            if (ExecSql.SqlExecNoquery(SqlStr, par) > 0)
                return true;
            else
                return false;
        }

        /// <summary>
        /// 生成随即密码 5 位数字
        /// </summary>
        /// <returns></returns>
        public static string CreatePassword()
        {
            Random r = new Random();
            int rl = r.Next(10000, 99999);
            return rl.ToString();
        }

        /// <summary>
        /// 密码初始化
        /// </summary>
        /// <param name="usrid">用户ID</param>
        /// <returns></returns>
        public string ResetUserPassword(int usrid)
        {
            string password = CreatePassword();
            string ErrMsg;
            if (ExecSql.SqlExecNoquery("update [hfg_company_user] set usrpass='" + password + "' where usrid='" + usrid + "'")>0)
                return password;
            else
                return string.Empty;
        }

        /// <summary>
        /// 判断是否存在相同卡号的用户存在
        /// </summary>
        /// <param name="usrnumb">用户卡号</param>
        /// <returns></returns>
        public bool CheckSameUser(string usrnumb)
        {
            int count = ExecSql.GetDataSet("select * from hfg_company_user where usrnumb = '" + usrnumb + "'").Tables[0].Rows.Count;
            if (count > 0)
                return true;
            else
                return false;
        }

        /// <summary>
        /// 删除公司客信息
        /// </summary>
        /// <param name="usrid">用户ID</param>
        /// <returns></returns>
        public bool CompnayUser_Del(int usrid)
        {
            string SqlStr = "delete from hfg_company_user where usrid ='" + usrid + "'";
            string ErrMsg;
            if (ExecSql.SqlExecNoquery(SqlStr)>0)
                return true;
            else
                return false;
        }

        /// <summary>
        /// 获取所属公司数据
        /// </summary>
        /// <returns></returns>
        public DataTable GetUserOfCompany(int Gid, int Pid)
        {
            string SqlStr;
            switch (LanguageID)
            {
                case 1:
                    SqlStr = "select corpid,corpname_e as corpname from [hfg_company] where gid='" + Gid + "' and pid='" + Pid + "' order by corpid desc";
                    break;
                default:
                    SqlStr = "select corpid,corpname as corpname from [hfg_company] where gid='" + Gid + "' and pid='" + Pid + "' order by corpid desc";
                    break;
            }
            return ExecSql.GetDataSet(SqlStr).Tables[0];
        }

        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        /// <param name="PageSize"></param>
        /// <param name="PageIndex"></param>
        /// <param name="StrWhere"></param>
        /// <returns></returns>
        public DataSet GetCompanyUserList(int PageSize, int PageIndex, string StrWhere)
        {
            return ExecSql.AddParamFrKeyField("hfg_company_user", "*", "indate", "usrid", PageSize, PageIndex, StrWhere);
        }

        /// <summary>
        /// 根据UserId获取DataRow
        /// </summary>
        /// <param name="UserId"></param>
        /// <returns></returns>
        public DataRow GetCompanyUserOne(int UserId)
        {
            return ExecSql.GetDataSet("SELECT * FROM [hfg_COMPANY_USER] WHERE usrid ='" + UserId + "'").Tables[0].Rows[0];
        }
    }
}
